package online.xybh.qa.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import online.xybh.qa.dao.CommonAnswerMapper;
import online.xybh.qa.dao.CommonOptionsMapper;
import online.xybh.qa.dao.CommonQuestionMapper;
import online.xybh.qa.model.*;
import online.xybh.qa.service.CommonQuestionService;
import online.xybh.qa.utils.InviteCodeGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 22:50 2020/10/23
 * @Modified:
 */
@Service
public class CommonQuestionServiceImpl implements CommonQuestionService {
    @Resource
    private CommonQuestionMapper commonQuestionMapper;
    @Resource
    private CommonOptionsMapper optionsMapper;
    @Resource
    private CommonAnswerMapper AnswerMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void createCommonQuestion(String title, Integer type, String answer, String parse, String[] options, Integer subject, String creator) {
        CommonQuestion commonQuestion = new CommonQuestion();
        commonQuestion.setId(InviteCodeGenerator.generateInviteCode());
        commonQuestion.setTitle(title);
        commonQuestion.setType(type);
        commonQuestion.setSubject(subject);
        commonQuestion.setCreator(creator);
        commonQuestion.setQuery(0);
        commonQuestion.setCreateTime(new Date());
        commonQuestionMapper.insertSelective(commonQuestion);
        CommonAnswer commonAnswer = new CommonAnswer();
        commonAnswer.setId(commonQuestion.getId());
        commonAnswer.setAnswer(answer);
        commonAnswer.setParse(parse);
        AnswerMapper.insert(commonAnswer);
        if (type == 1) {
            for (String option : options) {
                CommonOptions commonOptions = new CommonOptions();
                commonOptions.setId(commonQuestion.getId());
                commonOptions.setOptions(option);
                optionsMapper.insert(commonOptions);
            }

        }
    }

    @Override
    public PageInfo<CommonQuestion> queryAllCommonQuestion(Integer page, Integer size) {
        CommonQuestionExample commonQuestionExample = new CommonQuestionExample();
        commonQuestionExample.setOrderByClause("query desc");
        PageHelper.startPage(page, size);
        List<CommonQuestion> commonQuestions = commonQuestionMapper.selectByExample(commonQuestionExample);
        PageInfo<CommonQuestion> commonQuestionPageInfo = new PageInfo<>(commonQuestions);
        return commonQuestionPageInfo;
    }

    @Override
    public online.xybh.qa.dto.CommonQuestion queryCommonQuestionById(String id) {
        CommonQuestion commonQuestion = commonQuestionMapper.selectByPrimaryKey(id);
        commonQuestion.setQuery(commonQuestion.getQuery()+1);
        commonQuestionMapper.updateByPrimaryKey(commonQuestion);
        CommonAnswer commonAnswer = AnswerMapper.selectByPrimaryKey(id);
        CommonOptionsExample commonOptionsExample = new CommonOptionsExample();
        commonOptionsExample.createCriteria()
                .andIdEqualTo(id);
        List<CommonOptions> commonOptions = optionsMapper.selectByExample(commonOptionsExample);
        online.xybh.qa.dto.CommonQuestion question = new online.xybh.qa.dto.CommonQuestion();
        BeanUtils.copyProperties(commonQuestion, question);
        BeanUtils.copyProperties(commonAnswer, question);
        ArrayList<String> op = new ArrayList<>();
        for (CommonOptions options : commonOptions) {
            op.add(options.getOptions());
        }
        question.setOptions(op);
        return question;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteCommonQuestionById(String id) {
        commonQuestionMapper.deleteByPrimaryKey(id);
        AnswerMapper.deleteByPrimaryKey(id);
        CommonOptionsExample commonOptionsExample = new CommonOptionsExample();
        commonOptionsExample.createCriteria()
                .andIdEqualTo(id);
        optionsMapper.deleteByExample(commonOptionsExample);
    }
}
